package com.pan.kang.util;

import java.util.Arrays;
import java.util.Objects;
import java.util.function.BooleanSupplier;
import java.util.function.Supplier;

public class AccConditionUtils<T> {
    private final boolean result;

    public AccConditionUtils(boolean result) {
        this.result = result;
    }

    public static <T> AccConditionUtils<T> isAnyMatch(Boolean... booleans) {
        boolean result = Arrays.stream(booleans).filter(Objects::nonNull).anyMatch(Boolean::booleanValue);
        return new AccConditionUtils(result);
    }

    public static <T> AccConditionUtils<T> isAllMatch(Boolean... booleans) {
        boolean result = Arrays.stream(booleans).filter(Objects::nonNull).allMatch(Boolean::booleanValue);
        return new AccConditionUtils(result);
    }

    public static <T> AccConditionUtils<T> isAllMatch(BooleanSupplier... booleanSuppliers) {
        boolean result = Arrays.stream(booleanSuppliers).filter(Objects::nonNull).allMatch(BooleanSupplier::getAsBoolean);
        return new AccConditionUtils(result);
    }

    public static <T> AccConditionUtils<T> isAnyMatch(BooleanSupplier... booleanSuppliers) {
        boolean result = Arrays.stream(booleanSuppliers).filter(Objects::nonNull).anyMatch(BooleanSupplier::getAsBoolean);
        return new AccConditionUtils(result);
    }

    public T handleConditionWithReturn(Supplier<T> ifSupplier, Supplier<T> elseSupplier) {
        if (this.result && null != ifSupplier) {
            return ifSupplier.get();
        } else {
            return null == elseSupplier ? null : elseSupplier.get();
        }
    }

    public T handleConditionWithReturn(Supplier<T> ifSupplier) {
        return this.result && null != ifSupplier ? ifSupplier.get() : null;
    }

    public void handleCondition(Runnable ifRunnable, Runnable elseRunnable) {
        if (this.result && null != ifRunnable) {
            ifRunnable.run();
        } else if (null != elseRunnable) {
            elseRunnable.run();
        }
    }

    public void handleCondition(Runnable ifRunnable) {
        if (this.result && null != ifRunnable) {
            ifRunnable.run();
        }

    }
}

